package com.gtfrank.web.servlet.day3;

import com.gtfrank.web.servlet.pojo.User;
import com.gtfrank.web.servlet.service.IUserService;
import com.gtfrank.web.servlet.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ProjectName: sudacoursetrainging
 * @ClassName: UpdateUserServlet
 * @PackageName:com.gtfrank.web.servlet.day3
 * @Description: 更新用户
 * @Author: frank
 * @Date: 2021/4/29 15:04
 * @Version v1.0
 */
@WebServlet(value = "/user/UpdateUserServlet")
public class UpdateUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //修改成功显示所有用户   失败在本界面
        try{
            IUserService userService = new UserServiceImpl();
            int id = Integer.parseInt(req.getParameter("userid"));
            String name = req.getParameter("userName");
            String password = req.getParameter("password");
            User user = new User();
            user.setName(name);
            user.setPassword(password);
            user.setId(id);
            userService.update(user);
            req.getRequestDispatcher("/user/showUserListServlet").forward(req,resp);
        }catch (Exception e){
            e.printStackTrace();
            req.getRequestDispatcher("/user/UpdateUserServlet").forward(req,resp);
        }
    }
}
